package com.quanyan.entry;

import com.quanyan.factory.ServerPipelineFactory;
import com.quanyan.util.ApplicationUtil;
import com.quanyan.util.Configure;
import org.apache.log4j.Logger;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.springframework.beans.factory.InitializingBean;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;


/**
 * 
 * @ClassName: MiddlewareServerStartEntry
 * @Description: 启动类
 * @author chuntu tuchun168@163.com
 * @date 2016年5月3日 上午11:18:42
 *
 */
public class MiddlewareServerStartEntry implements InitializingBean{

	private final static Logger log=Logger.getLogger(MiddlewareServerStartEntry.class);


	@Override
	public void afterPropertiesSet() throws Exception {
		startServer();
	}
	public void startServer(){
		long s = System.currentTimeMillis();
		// 启动TCP监听
		ServerBootstrap bootstrap = new ServerBootstrap(
				new NioServerSocketChannelFactory(
						Executors.newCachedThreadPool(),
						Executors.newCachedThreadPool()));
		bootstrap.setPipelineFactory(new ServerPipelineFactory());
		bootstrap.setOption("child.tcpNoDelay", true);
		bootstrap.setOption("child.keepAlive", true);
		String ip = Configure.getPropertyBykey("listen_ip");
		int port = Integer.parseInt(Configure.getPropertyBykey("listen_port"));
		bootstrap.bind(new InetSocketAddress(port));
		long e = System.currentTimeMillis();

		log.info("server startup in " + " port:" + port
				+ "　" + (e - s) + "ms.");
	}

	public static void main(String[] args) {
		long s = System.currentTimeMillis();
		ApplicationUtil.getInstance();
		long e = System.currentTimeMillis();
		log.info("server startup in " + (e - s) + "ms.");

	}

}
